package com.liukai.java_jxgl.controller;


import com.liukai.java_jxgl.common.Result;
import com.liukai.java_jxgl.model.SystemUser;
import com.liukai.java_jxgl.service.IUserService;
import com.liukai.java_jxgl.utils.ConstantsUtil;
import com.liukai.java_jxgl.utils.LoginMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService iUserService;

    @GetMapping("/login")
    public Result login(Integer userId,
                        String userPwd,
                        Integer userType) {
        Result result = new Result();
        int code = ConstantsUtil.SUCCESS;
        String msg = LoginMsg.LOGIN_SUCCESS.name();
//        System.out.println(userId);
        SystemUser systemUser = new SystemUser();
        systemUser.setUserId(userId);
        systemUser.setUserPwd(userPwd);
        systemUser.setUserType(userType);
        System.out.println(systemUser);
        SystemUser login = iUserService.login(systemUser.getUserId(), systemUser.getUserPwd());

        if (login == null) {
            System.out.println("用户不存在");
            code = ConstantsUtil.FAIELD;
            msg = LoginMsg.LOGIN_FAILED_NO_USER.name();
            result = Result.fail(code, msg, null);
        } else if (login.getUserPwd().equals(systemUser.getUserPwd())
                && login.getUserType() == systemUser.getUserType()) {
            System.out.println("登录成功");
            code = ConstantsUtil.SUCCESS;
            msg = LoginMsg.LOGIN_SUCCESS.name();
            result = Result.succ(code, msg, login);
        } else {
            if (!login.getUserPwd().equals(systemUser.getUserPwd())) {
                System.out.println("登录失败");
                code = ConstantsUtil.FAIELD;
                msg = LoginMsg.LOGIN_FAILED_ERROR_PWD.name();
                result = Result.fail(code, msg, null);
            }else if (login.getUserPwd().equals(systemUser.getUserPwd())
                    && login.getUserType() != systemUser.getUserType()){
                System.out.println("登录失败");
                code = ConstantsUtil.FAIELD;
                msg = LoginMsg.LOGIN_FAILED_TYPE_ERROR.name();
                result = Result.fail(code, msg, null);
            }
        }

        return result;
    }

    @GetMapping("/findAllUser")
    public Result findAllUser() {
        return Result.succ(iUserService.getAllUser());
    }

}
